package com.longge;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.TreeSet;

public class BeautifulIndices {

    public List<Integer> beautifulIndices(String s, String a, String b, int k) {
        HashSet<Integer> aSet = new HashSet<>();
        HashSet<Integer> bSet = new HashSet<>();
        for (int i = 0; i < s.length(); i++) {
            if (s.startsWith(a, i)) {
                aSet.add(i);
            }
            if (s.startsWith(b, i)) {
                bSet.add(i);
            }

        }

        TreeSet<Integer> ans = new TreeSet<>();
        for (Integer aIndex : aSet) {
            for (Integer bIndex : bSet) {
                if (Math.abs(aIndex - bIndex) <= k) {
                    ans.add(aIndex);
                }
            }
        }
        return new ArrayList<>(ans);
    }


    public static void main(String[] args) {
        /**
         * "frkxslnnn"
         * "rkxsl"
         * "n"
         * 2
         */
        new BeautifulIndices().beautifulIndices("frkxslnnn", "rkxsl", "n", 2);
    }
}
